home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvitovdu32 / src / pascal / unixio.c < prev    next >
C/C++ Source or Header  |  1991-11-10  |  2KB  |  124 lines

  1. /* Alex Dickinson
  2.    Procedures for setting and resetting UNIX tty characteristics.
  3.    Interesting functions are:
  4.       savetty;
  5.       restoretty;
  6.       echoon;
  7.       echooff;
  8.       singlecharon;
  9.       singlecharoff;
  10.       buffercount;
  11.       suspend;
  12.    A side effect of calling savetty is to set up signal handling to reset
  13.    the terminal characteristics appropriately for the various interrupt
  14.    signals.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <sgtty.h>
  19. #include <signal.h>
  20.  
  21. struct sgttyb initchars; /* store initial terminal characteristics */
  22.  
  23. /* Save the original tty characteristics and set up the signalling. */
  24. savetty()
  25. {
  26.   ioctl(1, TIOCGETP, &initchars);
  27.   setsignals();
  28. }
  29.  
  30. /* Restore the original tty characteristics. */
  31. restoretty()
  32. {
  33.   ioctl(1, TIOCSETN, &initchars);
  34. }
  35.  
  36. /* Set driver to read characters as they are typed without waiting for a
  37.    terminator. Echo remains unchanged. */
  38. singlecharon()
  39. {
  40.   struct sgttyb s;
  41.   ioctl(1, TIOCGETP, &s);
  42.   s.sg_flags |= CBREAK;
  43.   ioctl(1, TIOCSETN, &s);
  44.   fflush(stdin);
  45. }
  46.  
  47. /* Turn off single character read mode. */
  48. singlecharoff()
  49. {
  50.   struct sgttyb s;
  51.   ioctl(1, TIOCGETP, &s);
  52.   s.sg_flags &= ~CBREAK;
  53.   ioctl(1, TIOCSETN, &s);
  54. }
  55.  
  56. /* Turn character echoing on. */
  57. echoon()
  58. {
  59.   struct sgttyb s;
  60.   ioctl(1, TIOCGETP, &s);
  61.   s.sg_flags |= ECHO;
  62.   ioctl(1, TIOCSETN, &s);
  63. }
  64.  
  65. /* Turn character echoing off. */
  66. echooff()
  67. {
  68.   struct sgttyb s;
  69.   ioctl(1, TIOCGETP, &s);
  70.   s.sg_flags &= ~ECHO;
  71.   ioctl(1, TIOCSETN, &s);
  72.  }
  73.  
  74. /* Return the number of characters currently in the input buffer. */
  75. int buffercount()
  76. {
  77.   long count;
  78.   ioctl(1, FIONREAD, &count);
  79.   return(count);
  80. }
  81.  
  82. /* Yukko */
  83. realungetc(ch, filed)
  84.      char ch;
  85.      int filed;
  86. {
  87.   ioctl(filed, TIOCSTI, &ch);
  88. }
  89.  
  90. /* Catch signals from tty.
  91.    If sig is an interrupt, put 0 and terminator into the buffer.
  92.    Otherwise it was a suspend, so we put 1 and terminator into buffer. */
  93. handleint(sig)
  94.      int sig;
  95. {
  96.   fflush(stdin);
  97.   if (sig == SIGINT)
  98.     {
  99.       realungetc(0, 0);
  100.       realungetc(13, 0);
  101.     }
  102.   else
  103.     {
  104.       realungetc(1, 0);
  105.       realungetc(13, 0);
  106.     }
  107. }
  108.  
  109. /* Signal initialization. */
  110. setsignals()
  111. {
  112.   signal(SIGINT, handleint);
  113.   signal(SIGTSTP, handleint);
  114. }
  115.  
  116. /* Suspend the process */
  117. suspend()
  118. {
  119.   signal(SIGTSTP, SIG_DFL);
  120.   kill(0, SIGTSTP);
  121.   /* resumed again, goody! */
  122.   setsignals();
  123. }
  124.